<?php
class AIconSmsKute extends CActiveRecord
{
    public function InsertCategory($data)
    {
        $sql = "INSERT INTO c_category_icon_sms_kute SET 
            title=:title
            , position=:position
            , status=:status
            , create_date=:create_date
        ";
        
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();      
        
        $categoryId = Yii::app()->db->getLastInsertID();
        return $categoryId;
    }
    
    public function UpdateCategory($data)
    {
        $sql = "UPDATE c_category_icon_sms_kute SET 
            title=:title
            , position=:position
            WHERE id=:id
        ";
        
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();          
        return $result;    
    }
    
    public function GetAllCategory($currentPage, $numberRecordPerPage)
    {
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $sql = "SELECT * FROM c_category_icon_sms_kute ORDER BY position DESC, create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }
    
    public function GetTotalCategory()
    {
        $sql = "SELECT count(id) FROM c_category_icon_sms_kute";
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();
        return $rows["count(id)"];
    }
    
    public function QuickUpdateCategory($data)
    {
        $sql = "UPDATE c_category_icon_sms_kute SET 
            position=:position
            , status=:status
            WHERE id=:id
        ";
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();
        return $result;
    }
    
    public function DeleteCategory($id)
    {
        $sql = "DELETE FROM c_category_icon_sms_kute WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
    
    public function GetCategoryInfo($id)
    {
        $sql = "SELECT * FROM c_category_icon_sms_kute WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->queryRow();
        return $result;
    }
    
    public function GetCategory()
    {
        $sql = "SELECT * FROM c_category_icon_sms_kute ORDER BY position DESC, create_date DESC";
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->queryAll();
        return $result;
    }
    
    /*
    * Biểu tượng
    * 
    */
    
    public function GetAllIcon($search, $currentPage, $numberRecordPerPage)
    {
        $condition = "";
        $condition .= !empty($search["categoryId"]) ? " AND categoryId=" . $search["categoryId"] : "";
        $condition .= $search["status"]!="" ? " AND `status`='" . $search["status"] . "'" : "";
        
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $sql = "SELECT * FROM c_icon_sms_kute WHERE 1 " . $condition . " ORDER BY position DESC, create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        
        $list_category_id = "0";
        foreach($rows as $row){
            $list_category_id .= "," . $row["categoryId"];    
        }
        
        $list_category_id = Common::remove_duplicate($list_category_id);
        $sql = "SELECT id, title FROM c_category_icon_sms_kute WHERE id IN (" . $list_category_id . ")";
        $command = Yii::app()->db->createCommand($sql);
        $rowsCategory = $command->queryAll();
        $replaceCategory = array();
        
        foreach($rowsCategory as $row){
            $replaceCategory[$row["id"]] = $row["title"];
        }
        
        $i = 0;
        while(isset($rows[$i])){
            $rows[$i]["category_name"] = isset($replaceCategory[$rows[$i]["categoryId"]]) ? $replaceCategory[$rows[$i]["categoryId"]] : "";
            $i++;
        }
        
        return $rows;
    }
    
    public function GetTotalIcon($search)
    {
        $condition = "";
        $condition .= !empty($search["categoryId"]) ? " AND categoryId=" . $search["categoryId"] : "";
        $condition .= $search["status"]!="" ? " AND `status`='" . $search["status"] . "'" : "";
                
        $sql = "SELECT count(id) FROM c_icon_sms_kute WHERE 1 " . $condition;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();
        return $rows["count(id)"];
    }
    
    public function InsertIcon($data)
    {
        $sql = "INSERT INTO c_icon_sms_kute SET 
            categoryId=:categoryId
            , picture=:picture
            , position=:position
            , status=:status
            , create_date=:create_date
        ";
        
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();      
        
        $iconId = Yii::app()->db->getLastInsertID();
        return $iconId;      
    }
    
    public function UpdateIcon($data)
    {
        $sql = "UPDATE c_icon_sms_kute SET 
            categoryId=:categoryId
            , position=:position            
            WHERE id=:id
        ";
        
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();              
        return $result;   
    }
    
    public function QuickUpdateIcon($data)
    {
        $sql = "UPDATE c_icon_sms_kute SET 
            position=:position
            , status=:status
            , isHot=:isHot
            , date_of_hot=:date_of_hot
            WHERE id=:id
        ";
        $command = Yii::app()->db->createCommand($sql);
        $command->bindValues($data);
        $result = $command->execute();
        return $result;
    }
    
    public function DeleteIcon($id)
    {
        $sql = "DELETE FROM c_icon_sms_kute WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
    
    public function GetIconInfo($id)
    {
        $sql = "SELECT * FROM c_icon_sms_kute WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow();
        return $rows;   
    }
}  
?>